layui.config({
  base: Yacon.getRootPath()+"/public/js/skin_layui/modules/"
});

layui.use(['layer', 'element', 'form', 'sidemenu'], function() {
	
    var $ = layui.jquery,
    layer = layui.layer;
    element = layui.element,
    sidemenu = layui.sidemenu;
 
  // 隐藏左侧菜单
  var icon = $("#menu-btn i");
  $("#menu-btn").click(function() {
    $("#menu-btn i").toggleClass("icon-list");
    if ($(".layui-side").hasClass("sidebar-mini")) {
      $(".layui-side").animate({
        width: '200px'
      });
      $(".layui-body").animate({
        left: '200px'
      });
      $(".layui-footer").animate({
        left: '200px'
      });
    } else {
      $(".layui-side").animate({
        width: '55px'
      });
      $(".layui-body").animate({
        left: '55px'
      });
      $(".layui-footer").animate({
        left: '55px'
      });
    }
    $(".layui-side").toggleClass("sidebar-mini");
  });

  //添加tips
  sidemenu.showTips();

  $(".sysmenu").find("dd a").each(function() {

    var $this = $(this);
    
    $this.click(function() {
    	
        sidemenu.cnf({
          elem: '.layui-side-scroll',
          url: Yacon.getRootPath()+'/portalService?commID=menu&appDetailCode='+$this.attr("appd-id")+"&tokenID="+lilyEns().encode()
        });
        
        sidemenu.render();
        
        $(".layui-side").removeClass("sidebar-mini");

        // 展示菜单
        if ($(".layui-side").css("width") == "50px") {
          icon.toggleClass("icon-list");
        }
        $(".layui-side").animate({
          width: '200px'
        });
        $(".layui-body").animate({
          left: '200px'
        });
        $(".layui-footer").animate({
          left: '200px'
        });
        sidemenu.showTips();
        
        //展示应用首页
        $(".layui-body").empty();
        
        //添加动作绑定
        addMenuEvents();
        
        //首页访问        
        $(".layui-body").css("height",(window.bodyHeight + 20)+"px");
		
		$(".layui-body").empty();	
		var originHtml = "";		
		originHtml += "<div style='height:"+(window.bodyHeight+15)+"px;'>";
		originHtml += "<div class='container-fluid content col' style='height:"+(window.bodyHeight+15)+"px;'></div>";
		originHtml += "</div>";
		$(".layui-body").append(originHtml);
        
        $.ajax({    			
			type: "POST",
		    url: Yacon.getRootPath()+'/portalService?commID=home&appDetailCode='+$this.attr("appd-id")+"&tokenID="+lilyEns().encode(),
		    processData: false,
		    async: false,
		    dataType: 'text', 
		    success: function(data){
		    	$(".layui-body .container-fluid").append(data);
		    }
		});
       
    });
  });
  
  //优选选中第1条信息
  $(".sysmenu").find("dd").eq(0).trigger("click");
  $(".sysmenu").find("dd a").eq(0).trigger("click");
  
  $('#Fullscreen a').bind('click', function() {
    var fullscreenElement =
      document.fullscreenElement ||
      document.mozFullScreenElement ||
      document.webkitFullscreenElement ||
      document.msFullscreenElement;
    
    if (fullscreenElement == null) {
      requestFullScreen();
      $(this).html("退出全屏");
    } else {
      exitFullscreen();
      $(this).html("进入全屏");
    }
    $("#Fullscreen").removeClass("layui-this");
  });
  
  $(".system-screen-auto").click(function(){
	    if($(window).height()>=700){
			$("body").addClass("mid");
		}else{
			$("body").removeClass("mid");
		}
  });
  $(".system-screen-zip-tall").click(function(){
	  $("body").removeClass("mid");
  });
  $(".system-screen-zip-mid").click(function(){
	  $("body").addClass("mid");
  });
});

//进入全屏
function requestFullScreen() {

  var de = document.documentElement;
  
  if (de.requestFullscreen) {
    de.requestFullscreen();
  } 
  else if (de.mozRequestFullScreen) {
    de.mozRequestFullScreen();
  } 
  else if (de.webkitRequestFullScreen) {
    de.webkitRequestFullScreen();
  } 
  else if (de.msRequestFullscreen) {
	de.msRequestFullscreen();
  } 
  else if (typeof window.ActiveXObject !== "undefined") {
	var wscript = new ActiveXObject("WScript.Shell");
    if(wscript != null) { 
        wscript.SendKeys("{F11}");
    }
  }
  
}
//退出全屏
function exitFullscreen() {
  var de = document;

  if (de.exitFullscreen) {
    de.exitFullscreen();
  } else if (de.mozCancelFullScreen) {
    de.mozCancelFullScreen();
  } else if (de.webkitCancelFullScreen) {
    de.webkitCancelFullScreen();
  } else if (de.msExitFullscreen) {
    de.msExitFullscreen();
  }
  else if (typeof window.ActiveXObject !== "undefined") {
      var wscript = new ActiveXObject("WScript.Shell");
      if (wscript !== null) {
          wscript.SendKeys("{F11}");
      }
  }
  
}

document.addEventListener('fullscreenchange', function(){
	window.bodyHeight = $(window).height() - 80;	
	$(".layui-body").css("height",(window.bodyHeight+20)+"px");
});

document.addEventListener('webkitfullscreenchange', function(){
	window.bodyHeight = $(window).height() - 80;	
	$(".layui-body").css("height",(window.bodyHeight+20)+"px");
});

document.addEventListener('mozfullscreenchange', function(){	
	window.bodyHeight = $(window).height() - 80;
	$(".layui-body").css("height",(window.bodyHeight+20)+"px");
});

document.addEventListener('MSFullscreenChange', function(){	
	window.bodyHeight = $(window).height() - 80;
	$(".layui-body").css("height",(window.bodyHeight+20)+"px");
});

//动作绑定 
function addMenuEvents(){
	
	$(".layui-side .layui-side-scroll dl dd a").unbind("click");
	$(".layui-side .layui-side-scroll dl dd a").click(function(){
		
		var self = $(this);
		
		if(!Yacon.isEmpty(window.singlePortalTime)){
			clearTimeout(window.singlePortalTime);
		}
		
		window.singlePortalTime = setTimeout(function(){
			
			window.bodyHeight = $(window).height() - 80;
			
			var menuID = self.attr("data-url");
			
			$(".layui-body").css("height",(window.bodyHeight+20)+"px");
			
			$(".layui-body").empty();	
			var originHtml = "";		
			originHtml += "<div style='height:"+(window.bodyHeight+15)+"px;'>";
			originHtml += "<div class='container-fluid content col' style='height:"+(window.bodyHeight+15)+"px;'></div>";
			originHtml += "</div>";
			$(".layui-body").append(originHtml);
			
			//已缓存
			if(!window.portalMenuPath[menuID]){
				
				$.ajax({
					
					type: "POST",
				    url: Yacon.getRootPath()+"/portalService?commID=url&menuID="+menuID+"&tokenID="+lilyEns().encode(),
				    processData: false,
				    async: false,
				    dataType: 'text', 
				    success: function(data){
				    	
				    	var menu = JSON.parse(data);
				    	
				    	window.portalMenuPath[menuID] = menu;
				    	window.portalAppPath[menu.appCode] = menu.appUrl;
				    				    	
				    }
				
				});
				
			}
				
			var menu = window.portalMenuPath[menuID];
			
			window.pwMenu = menuID;                          //菜单标示
	    	window.pwAppCode = menu.appCode;                 //访问系统标示
	    	window.pwCtxPath = "/" + menu.appCode;           //访问系统部署
	    	window.pwAppPath = menu.appUrl;                  //访问系统路径
	    	window.pwMenuPath = menu.menuUrl;                //访问菜单路径
	    	
	    	window.pwSessionID = menu.sessionID;

	    	var url = Yacon.addUrlParam("lilyPageType", "0", window.pwMenuPath);
	    	url = Yacon.addToken(url);
	    	
	    	//解绑注册对象
	    	Yacon.unload();
	    	
	    	//内部菜单
	    	if(menu.menuKind == "00"){
	    		
	    		$.ajax({    			
	    			type: "POST",
	    		    url: url,
	    		    processData: false,
	    		    async: false,
	    		    dataType: 'text', 
	    		    success: function(data){
	    		    	$(".layui-body .container-fluid").append(data);
	    		    }
	    		});
	    	}
	    	
	    	//外部菜单
	    	else{
	    		
	    		var html = "";
	    		
	    		html += "<iframe ";  		
	    		html += " src='"+url+"' ";
	    		html += " width='100%' ";
	    		html += " height='100%' ";
	    		html += " frameborder='0' ";   		
	    		html += ">";  		
	    		html += "</iframe>";
	    		
	    		$(".layui-body .container-fluid").append(html);
	    		
	    	}
			
	    	
		},300);
		
	});
	
}

/** 获取绝对路径 */
function getAbsolutePath(relaPath){
	
	var appUrl = window.pwAppPath;
	var menuUrl = window.pwMenuPath;	
	var contextPath = window.pwCtxPath;
	var sessionID = window.pwSessionID;
	
	//菜单路径不存在的情况
	if(Yacon.isEmpty(menuUrl)){
		return relaPath;
	}
	
	var resultUrl = "";
	
	if(relaPath.startsWith("../")){
		
		var firstURL = cutUrl(menuUrl.substring(0,menuUrl.lastIndexOf("/")), relaUrl);
		
		//后置路径，无开始符
		var sndURL = endUrl(relaUrl);
		
		resultUrl = firstURL + "/" + sndURL;
		
	}
	
	//根路径相对
	else if(relaPath.startsWith("./")){
		resultUrl = menuUrl.substring(0,menuUrl.lastIndexOf("/")) + relaPath.substring(1);
	}
	
	//绝对路径
	else if(relaPath.startsWith("http")){
		resultUrl = relaPath;
	}
	
	//应用下路径
	else if(relaPath.startsWith(contextPath)){		
		resultUrl = appUrl + relaPath.substring(contextPath.length);
	}
	
	//绝对路径
	else if(relaPath.startsWith("/")){
		resultUrl = appUrl + relaPath;
	}
	
	//菜单根路径
	else {		
		resultUrl = menuUrl.substring(0,menuUrl.lastIndexOf("/")) + "/" + relaPath;	
	}
	
	return resultUrl;
	
}

//路径截取
function cutUrl(oldUrl,relaUrl){
	if(relaUrl.startsWith("../")){
		oldUrl = oldUrl.substring(0,oldUrl.lastIndexOf("/"));
		return cutUrl(oldUrl, relaUrl.substring(3));
	}
	else{
		return oldUrl;
	}
}

//路径终结
function endUrl(relaUrl){
	if(relaUrl.startsWith("../")){
		relaUrl = relaUrl.substring(3);
		return endUrl(relaUrl);
	}
	else{
		return relaUrl;
	}
}
